package com.jinlejia.erp.ui.mine.assist;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.freedom.yefeng.yfrecyclerview.YfListInterface;
import com.freedom.yefeng.yfrecyclerview.YfListMode;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.base.BaseActivity;
import com.jinlejia.erp.base.MapCallBack;
import com.jinlejia.erp.databinding.ActivityAssistAignatureSearchBinding;
import com.jinlejia.erp.ui.mine.adapter.AssistantAdapter;
import com.jinlejia.erp.utils.DataFilter;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.Map;

import okhttp3.Call;
import okhttp3.MediaType;

/**
 * 协助签单搜索页
 */
public class AssistAignatureSearchActivity extends BaseActivity {

    private ArrayList<Map<String, Object>> dataList = new ArrayList<>();
    private AssistantAdapter adapter;
    private int type;
    private int state;
    private int userState;
    private String initiateTimeMin;
    private String initiateTimeMax;

    private ActivityAssistAignatureSearchBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_assist_aignature_search);
        binding.setEvent(this);
        initView();
        initEvent();

    }

    private void initEvent() {

        binding.etSearchText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (binding.etSearchText.getText().length() != 0) {
                    binding.btClear.setVisibility(View.VISIBLE);
                } else {
                    binding.btClear.setVisibility(View.GONE);
                }
                getData();
            }
        });

        adapter.setOnItemClickListener((view, o) -> {
            Intent intent = new Intent(AssistAignatureSearchActivity.this, AssistInfoActivity.class);
            intent.putExtra("id", ((Map<String, Object>) o).get("id").toString());
            intent.putExtra("userstate", ((Map<String, Object>) o).get("userstate").toString());
            startActivityForResult(intent, 1001);
        });
    }

    private void initView() {
        binding.yfAssistSearchResult.setHasFixedSize(true);
        binding.yfAssistSearchResult.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AssistantAdapter(dataList);
        adapter.changeMode(YfListMode.MODE_DATA);
        binding.yfAssistSearchResult.setAdapter(adapter);
        type = getIntent().getIntExtra("type", -1);
        state = getIntent().getIntExtra("state", -1);
        userState = getIntent().getIntExtra("userState", -1);
        initiateTimeMin = getIntent().getStringExtra("initiateTimeMin").toString();
        initiateTimeMax = getIntent().getStringExtra("initiateTimeMax").toString();
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_back:
                finish();
                break;
            case R.id.bt_clear:
                binding.etSearchText.setText("");
                dataList.clear();
                adapter.setData(dataList);
                break;
        }
    }

    private JSONObject jsonObject = new JSONObject();

    public void getData() {
        jsonObject.put("pageIndex", 1);
        jsonObject.put("pageSize", 5);
        String s = binding.etSearchText.getText().toString();
        if (!"".equals(s)) {
            jsonObject.put("initiatorName", s);
        } else {
            jsonObject.remove("initiatorName");
        }
        if (type != -1) {
            jsonObject.put("type", type);
        }
        if (state != -1) {
            jsonObject.put("state", state);
        }
        if (userState != -1) {
            jsonObject.put("userState", userState);
        }
        if (!"".equals(initiateTimeMin)) {
            jsonObject.put("initiateTimeMin", initiateTimeMin);
        } else {
            jsonObject.remove("initiateTimeMin");
        }

        if (!"".equals(initiateTimeMax)) {
            jsonObject.put("initiateTimeMax", initiateTimeMax);
        } else {
            jsonObject.remove("initiateTimeMax");
        }

        OkHttpUtils
                .postString()
                .url(ApiConfig.ASSIST_Assistant_LIST)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .content(jsonObject.toString())
                .build()
                .execute(new MapCallBack() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Map<String, Object> response, int id) {
                        if (DataFilter.filter(AssistAignatureSearchActivity.this, response, adapter)) {
                            JSONObject jsonObject = JSON.parseObject(response.toString());
                            dataList = JSON.parseObject(jsonObject.getJSONArray("data").toString(), new TypeReference<ArrayList<Map<String, Object>>>() {
                            });
                            adapter.setData(dataList);
                        } else if (response.get("code").toString().equals("501")) {
                            adapter.changeMode(YfListMode.MODE_EMPTY);
                        }
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        getData();
    }
}
